package com.xapp.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;

/**
 * @author YH_CK
 * @data 2018.09.14 14:31
 * @describe 权限判断和获取处理工具
 **/
public class PermissionUtils {
    private static final String TAG = PermissionUtils.class.getSimpleName();
    private Context mContext;
    private Handler mHandler;

    public PermissionUtils(Context context, Handler handler) {
        mContext = context;
        mHandler = handler;
    }

//    public static PermissionUtils getInstance(Context context, Handler handler) {
//        if (instance == null) {
//        instance = new PermissionUtils();
//        }
//        mContext = context;
//        mHandler = handler;
//        return instance;
//    }

    /**
     * @param permissionName 权限名称
     * @param requestCode
     * @param messageCode
     */
    public void needPermission(String permissionName, int requestCode, int messageCode) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            this.mHandler.sendEmptyMessage(messageCode);
            return;
        } else {
            /**
             * 处理小米等手机权限再次封装问题，暂时不用
             */
            //          AppOpsManager appOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
//            int checkOp = appOpsManager.checkOp(AppOpsManager.OPSTR_READ_CONTACTS, android.os.Process.myUid(), mContext.getPackageName());
//            switch (checkOp) {
//                case AppOpsManager.MODE_ALLOWED:
//                    Log.e(TAG, "AppOpsManager.MODE_ALLOWED ：有权限");
//                    break;
//                case AppOpsManager.MODE_IGNORED:
//                    Log.e(TAG, "AppOpsManager.MODE_IGNORED：被禁止了");
//                    break;
//                case AppOpsManager.MODE_DEFAULT:
//                    Log.e(TAG, "AppOpsManager.MODE_DEFAULT");
//                    break;
//                case AppOpsManager.MODE_ERRORED:
//                    Log.e(TAG, "AppOpsManager.MODE_ERRORED：出错了");
//                    break;
//                case 4:
//                    Log.e(TAG, "AppOpsManager.OTHER：权限需要询问");
//                    break;
//            }
            requestPermissions(permissionName, requestCode, messageCode);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void requestPermissions(String permissionName, int requestCode, int messageCode) {
        String locFlag = (String) SharedPreferencesUtils.getParam(mContext, "checLoaction", "-1");

        if (((Activity) mContext).checkSelfPermission(permissionName) != PackageManager.PERMISSION_GRANTED) {
            if (permissionName.equals(Manifest.permission.ACCESS_COARSE_LOCATION) && locFlag.equals("1")) {
                this.mHandler.sendEmptyMessage(1022);
            } else {
                ((Activity) mContext).requestPermissions(new String[]{permissionName}, requestCode);
            }
        } else {
            this.mHandler.sendEmptyMessage(messageCode);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean hasPermissions(String permissions) {
        if (((Activity) mContext).checkSelfPermission(permissions) != PackageManager.PERMISSION_GRANTED) {
           return false;
        } else {
            return true;
        }
    }
}
